<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name">
								<template #label>
									<WtmLabelTooltip label="仓库名称" tooltip="仓库名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Name" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Code_FormItem" prop="Entity.Code">
								<template #label>
									<WtmLabelTooltip label="仓库编码" tooltip="仓库编码" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Code" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ErpId_FormItem" prop="Entity.ErpId">
								<template #label>
									<WtmLabelTooltip label="ERP编号" tooltip="ERP系统中的编号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ErpId" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Location_FormItem" prop="Entity.Location">
								<template #label>
									<WtmLabelTooltip label="位置" tooltip="仓库物理位置" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Location" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_IsVirtual_FormItem"
								prop="Entity.IsVirtual"
								:rules="[{ required: true, message: '是否虚拟仓库为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="是否虚拟仓库" tooltip="是否为虚拟仓库" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.IsVirtual" disabled class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_CommonState_FormItem"
								prop="Entity.CommonState"
								:rules="[{ required: true, message: '状态为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="状态" tooltip="仓库状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.CommonState" disabled class="width100">
									<el-option key="启用" value="启用" label="启用"></el-option>
									<el-option key="禁用" value="禁用" label="禁用"></el-option>
									<el-option key="废弃" value="废弃" label="废弃"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_Description_FormItem" prop="Entity.Description">
								<template #label>
									<WtmLabelTooltip label="描述" tooltip="仓库描述信息" />
								</template>
								<el-input type="textarea" :rows="3" v-model="stateData.vmModel.Entity.Description" disabled></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>管理信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="24" class="mb20">
							<WtmOrgSelector
								v-model="stateData.vmModel.Entity.OrgId"
								label="库存组织"
								prop="Entity.OrgId"
								tooltip="选择仓库所属的库存组织"
								:auth="true"
								:rules="[{ required: true, message: '公司为必填项', trigger: 'blur' }]"
								disabled
							/>
						</el-col>

						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="SelectedStockersIds_FormItem" prop="SelectedStockersIds">
								<template #label>
									<WtmLabelTooltip label="库管理员" tooltip="仓库管理员列表" />
								</template>
								<el-transfer
									v-model="stateData.vmModel.SelectedStockersIds"
									disabled
									:data="stateData.AllFrameworkUsers"
									:props="{ key: 'Value', label: 'Text' }"
									:titles="['可选库管理员', '已选库管理员']"
								></el-transfer>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysWarehouseApi as api } from '/@/views/supplychain/syswarehouse/api';
import other from '/@/utils/other';
import { ISysWarehouseVm } from '/@/views/supplychain/syswarehouse/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysWarehouseVm>>({
	vmModel: {
		Entity: {
			ID: null,
			Name: null,
			Code: null,
			ErpId: null,
			OrgId: null,
			Description: null,
			Location: null,
			IsVirtual: false,
			CommonState: '启用',
		},
		SelectedStockersIds: [],
		ActionName: null,
		IsMyApprove: null,
	},

	AllFrameworkUsers: [] as ISysSelectType[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	// 先加载仓库详情数据
	await api()
		.get(stateData.vmModel.Entity.ID ?? '')
		.then((x) => {
			other.setValue(stateData.vmModel, x);
			// 如果有 OrgId，加载对应的管理人员列表
			if (stateData.vmModel.Entity.OrgId) {
				return other
					.getSelectList('/api/SysWarehouse/GetFrameworkUsers?orgId=' + stateData.vmModel.Entity.OrgId, [], false)
					.then((users) => (stateData.AllFrameworkUsers = users));
			}
		});
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
